﻿Public Class frmFileInfo
    ''' <summary>
    ''' 添加记录
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmdAddRec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddRec.Click
        Dim NewRec As New FileInfoRec
        If txtProgramID.Text = String.Empty Then
            Exit Sub
        End If
        With NewRec
            .ProgID = txtProgramID.Text
            .FileID = txtFileID.Text
            .Length = NUDLength.Value
            If radFix.Checked Then
                .RECFM = enmRECFM.FB
            Else
                .RECFM = enmRECFM.VB
            End If
            .IsExternal = chkExternal.Checked
            .IO = Me.cmbIO.SelectedIndex
            Dim strKey As String = .ProgID & "|" & .FileID
            If mFileInfoDic.ContainsKey(strKey) Then
                mFileInfoDic.Remove(strKey)
            End If
            mFileInfoDic.Add(strKey, NewRec)
        End With
        InitlizeForm()
    End Sub
    ''' <summary>
    ''' 删除选中记录
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmdDeleteRec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDeleteRec.Click
        Dim strKey As String = txtProgramID.Text & "|" & txtFileID.Text
        If mFileInfoDic.ContainsKey(strKey) Then
            mFileInfoDic.Remove(strKey)
        End If
        InitlizeForm()
    End Sub
    ''' <summary>
    ''' 保存窗体
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Dim mFileInfoList As New clsFileInfoList
        Dim SaveFileList As New List(Of CommonMethod.FileInfoRec)
        For Each mFileInfo As CommonMethod.FileInfoRec In mFileInfoDic.Values
            SaveFileList.Add(mFileInfo)
        Next
        mFileInfoList.FileInfo = SaveFileList
        WriteObjectToXMLFile(mFileInfoList, GetType(clsFileInfoList), MyConfig.FileInfoFileName)
    End Sub
    ''' <summary>
    ''' 加载窗体
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub frmFileInfo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim mFileInfoList As New clsFileInfoList
            mFileInfoList = ReadObjectFromXMLFile(GetType(clsFileInfoList), MyConfig.FileInfoFileName)
            mFileInfoDic.Clear()
            For Each mFile As CommonMethod.FileInfoRec In mFileInfoList.FileInfo
                mFileInfoDic.Add(mFile.ProgID & "|" & mFile.FileID, mFile)
            Next
        Catch ex As Exception
            MsgBox("Can't Open File")
        End Try
        InitlizeForm()
    End Sub
    ''' <summary>
    ''' 重新初始化界面元素
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub InitlizeForm()
        Me.cmbIO.SelectedIndex = 0
        Me.txtFileID.Text = String.Empty
        Me.txtProgramID.Text = String.Empty
        Me.NUDLength.Value = 0
        Me.chkExternal.Checked = False
        Me.radFix.Checked = True
        lstFileInfo.Items.Clear()
        Dim FileIDList As New List(Of String)
        For Each strFileId As String In mFileInfoDic.Keys
            FileIDList.Add(strFileId)
        Next
        FileIDList.Sort()
        For Each strFileId As String In FileIDList
            With mFileInfoDic(strFileId)
                Dim subItem As New ListViewItem
                subItem.Text = .ProgID
                subItem.SubItems.Add(.FileID)
                subItem.SubItems.Add(.RECFM.ToString)
                subItem.SubItems.Add(.Length)
                subItem.SubItems.Add(cmbIO.Items(.IO))
                subItem.SubItems.Add(.IsExternal)
                lstFileInfo.Items.Add(subItem)
            End With
        Next
    End Sub
    ''' <summary>
    ''' 列表选中记录发生改变
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub lstFileInfo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstFileInfo.SelectedIndexChanged
        If lstFileInfo.SelectedItems.Count > 0 Then
            Dim SelectFileName As String = lstFileInfo.SelectedItems(0).Text & "|" & lstFileInfo.SelectedItems(0).SubItems(1).Text
            With mFileInfoDic(SelectFileName)
                Me.txtProgramID.Text = .ProgID
                Me.txtFileID.Text = .FileID
                Me.NUDLength.Value = .Length
                Me.chkExternal.Checked = .IsExternal
                Me.cmbIO.SelectedIndex = .IO
                If .RECFM = enmRECFM.FB Then
                    radFix.Checked = True
                Else
                    radVarient.Checked = True
                End If
            End With
        End If
    End Sub
    ''' <summary>
    ''' 退出窗体
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        Me.Close()
    End Sub
End Class